﻿#region

using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Radovici.Ctrls;
using Radovici.Ctrls.Hacks;
using YouTubePlaylist.Models;
using YouTubePlaylist.MusicVideoService;
using YouTubePlaylist.ViewModels;

#endregion

namespace YouTubePlaylist.Views
{
    public partial class TracksCtrl
    {
        #region Private fields

        private PlaylistViewModel _vm;

        #endregion

        public TracksCtrl()
        {
            InitializeComponent();
            Loaded += OnLoaded;
            ViewModel = new PlaylistViewModel();
        }

        public PlaylistViewModel ViewModel
        {
            get { return _vm; }
            private set
            {
                _vm = value;
                var element = Content as FrameworkElement;
                if (element != null)
                {
                    element.DataContext = _vm;
                }
            }
        }

        #region Playlist dependency property

        public static readonly DependencyProperty PlaylistModelProperty = DependencyProperty.Register("PlaylistModel",
                                                                                                      typeof (
                                                                                                          PlaylistModel),
                                                                                                      typeof (TracksCtrl
                                                                                                          ),
                                                                                                      new PropertyMetadata
                                                                                                          (null,
                                                                                                           OnPlaylistModelChanged));

        public PlaylistModel PlaylistModel
        {
            get { return (PlaylistModel) GetValue(PlaylistModelProperty); }
            set { SetValue(PlaylistModelProperty, value); }
        }

        private static void OnPlaylistModelChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var ctrl = d as TracksCtrl;
            if (ctrl != null)
            {
                ctrl.ViewModel.PlaylistModel = e.NewValue as PlaylistModel;
            }
        }

        #endregion

        private void OnLoaded(object sender, RoutedEventArgs e)
        {
            //this.ApplyTheme("RainierOrange.xaml");
        }

        private void OnTrackSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var grid = sender as DataGridPlus;
            if (grid != null)
            {
                ViewModel.PlaylistModel.SelectedTrack = grid.SelectedItem as VideoTrack;
            }
        }

        private void OnMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            var grid = sender as DataGridPlus;
            if (grid != null)
            {
                ViewModel.PlaylistModel.SelectedTrack = grid.SelectedItem as VideoTrack;
            }
        }

        private void OnDoubleClicked(object sender, MouseButtonEventArgs e)
        {
            var data = sender as DataGrid;
            if (data != null)
            {
                ViewModel.PlaylistModel.Current = data.SelectedItem as VideoTrack;
            }
        }

        private void OnTracksKeyUp(object sender, KeyEventArgs e)
        {
            switch (e.Key)
            {
                case Key.Delete:
                    ViewModel.PlaylistModel.DeleteTrack(ViewModel.PlaylistModel.SelectedTrack);
                    break;
            }
        }
    }
}